typedef struct stat STAT;

int get_statistics(char* pathName, STAT* outStats)
{
	LOC inodeLoc;
	MINODE* mip;
	
	if (pathName[0] == '/')
	{
		inodeLoc = get_inode_number(pathName);
		if (inodeLoc.inodeNum == 0)
		{
			printf("\"%s\" could not be found.\n", pathName);
			return -1;
		}
		mip = get_inode(inodeLoc.deviceHandle, inodeLoc.inodeNum);
		
		outStats->st_dev = inodeLoc.deviceHandle;
		outStats->st_ino = inodeLoc.inodeNum;
		
		outStats->st_mode = mip->inode.i_mode;
		outStats->st_nlink = mip->inode.i_links_count;
		outStats->st_uid = mip->inode.i_uid;
		outStats->st_gid = mip->inode.i_gid;
		outStats->st_size = mip->inode.i_size;
		outStats->st_blocks = mip->inode.i_blocks;
		outStats->st_atime = mip->inode.i_atime;
		outStats->st_ctime = mip->inode.i_ctime;
		outStats->st_mtime = mip->inode.i_mtime;
		
		put_inode(mip);
		return 1;
	}
	return -1;
}

int do_stat(char* pathName)
{	
	char fullPath[MAX_PATH_LENGTH];
	STAT stats;
	
	if (strlen(pathName) == 0)
		get_pwd_name(runningProc->cwd, fullPath);
	else if (pathName[0] != '/')
	{
		get_pwd_name(runningProc->cwd, fullPath);
		strcat(fullPath, pathName);
	}
	else
		strcpy(fullPath, pathName);
	
	if (get_statistics(fullPath, &stats) == -1)
		return -1;
	
	printf("  File: '%s'\n", fullPath);
	printf("  Size: %-10d Sectors: %-10d", (int)stats.st_size, (int)stats.st_blocks);
	if (stats.st_mode & IMODE_DIRECTORY)
		printf("Directory\n");
	else
		printf("Regular file\n");
	printf("Device: %-10d Inode: %-10d  Links: %d\n", (int)stats.st_dev, (int)stats.st_ino, (int)stats.st_nlink);
	printf("Access: "); print_inode_mode((int)stats.st_mode);
	printf("Uid: %-10d    Gid: %d\n", (int)stats.st_uid, (int)stats.st_gid);
	printf("Accessed: %s", ctime(&(stats.st_atime)));
	printf("Modified: %s\n", ctime(&(stats.st_mtime)));
	
	return 1;
}
